package com.itg.vms.servlet;

import com.alibaba.fastjson.JSONObject;
import com.itg.vms.entity.User;
import com.itg.vms.service.SysMenuService;
import com.itg.vms.service.impl.SysMenuServiceImpl;
import com.itg.vms.util.AjaxResult;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zjzaki
 * @create 2023年03月07日 01:49:11
 */
@WebServlet("/menu")
public class MenuServlet extends BaseServlet {

    private SysMenuService sysMenuService = new SysMenuServiceImpl();

    /**
     * 得到所有的菜单
     *  @author zjzaki
     */
    public void menu(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //得到session中的用户
        User loginUser = (User) req.getSession().getAttribute("loginUser");
        System.out.println(loginUser);
        //判断是否登录
        if (loginUser == null) {
            resp.getWriter().write(JSONObject.toJSONString(AjaxResult.error(302,"../login.html")));
        }else if("1".equals(loginUser.getUserType())){
            System.out.println(loginUser);
            // 没有管理权限
            resp.getWriter().write(JSONObject.toJSONString(AjaxResult.error(403,"权限呢？？，还想进管理页面!!!")));
        } else{
            System.out.println(success(sysMenuService.menu()));
            resp.getWriter().write(success(sysMenuService.menu()));
        }
    }
}
